package com.njbw.design_pattern.created.prototype.deepCopy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 促销活动
 * String 和 LocalDate 是不可变对象，直接赋值即可
 *
 * @author 黄侯淳
 * @since JDK 1.8
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PromotionEvent implements Cloneable, Serializable {

    /**
     * 促销活动名称
     */
    private String name;
    /**
     * 促销开始时间
     */
    private LocalDate startDate;
    /**
     * 促销结束时间
     */
    private LocalDate endDate;
    /**
     * 促销规则列表
     */
    private List<PromotionRule> ruleList = new ArrayList<>();

    public void addRule(PromotionRule rule) {
        ruleList.add(rule);
    }

    @Override
    protected PromotionEvent clone() throws CloneNotSupportedException {
        PromotionEvent cloned = (PromotionEvent) super.clone();
        // 深拷贝规则列表
        if (this.ruleList != null && !this.ruleList.isEmpty()) {
            cloned.setRuleList(this.ruleList.stream().map(PromotionRule::clone).collect(Collectors.toList()));
        }
        return cloned;
    }
}
